package com.fz.main;

import com.fz.dao.BookDao;
import com.fz.entity.Book;
import com.fz.mapper.BookMapper;
import com.fz.mybatis.MybaisHelp;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by webrx on 2017-08-23.
 */
public class Test {
    public static void main(String[] args) {

/*//建立数据库会话工厂
        SqlSessionFactory sf = null;
        try {
            sf = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("book.xml"));
            System.out.println(sf);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //读取会话
        SqlSession s = sf.openSession();
        BookMapper bo = s.getMapper(BookMapper.class);

        Book book = new Book();
        book.setName("java");
        book.setPrice(100);


        int m = bo.insert(book);
        System.out.println(m);


        List<Book> books = bo.queryall();
        for (Book b: books) {
            System.out.println(b.getName());
        }*/
       // s.commit();


        /**
         * 工具类调用
         *
         */
        BookMapper bookMapper = new MybaisHelp<BookMapper>(BookMapper.class).getMapper();
        List<Book> books = bookMapper.queryall();
        for (Book b: books) {
            System.out.println(b.getName());
        }
        /*MybaisHelp<BookMapper> bo = new MybaisHelp<BookMapper>(BookMapper.class);
        BookMapper boo = bo.getMapper();*/



        /**
         * dao实现类调用
         */

        BookDao bookDao = new BookDao();

        //insert
        Map<String,Object> map = new HashMap<String,Object>();

        map.put("name","<html>");
        map.put("price",20);


        int m = bookDao.save(map);
        System.out.println(m);
        //select
        List<Book> bok = bookDao.queryall();
        for (Book b: bok) {
            System.out.println(b.getName()+"**");
        }

        /**
         * 执行完insert不能借着执行select不然会报错
         */
        BookDao bookDaos = new BookDao();
       /* int a = bookDaos.saves("<php>",20);
        System.out.println(a);*/
        List<Book> boks = bookDaos.queryall();
        for (Book b: boks) {
            System.out.println(b.getName()+"--");
        }
    }
}
